package com.hooker.common;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Reflection {

    public static Object filed(Object obj, String className, String filedName) {
        try {
            Class cls = Class.forName(className);
            Field field = cls.getDeclaredField(filedName);
            field.setAccessible(true);
            return field.get(obj);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Object set(Object obj, String className, String filedName, Object value) {
        try {
            Class cls = Class.forName(className);
            Field field = cls.getDeclaredField(filedName);
            field.setAccessible(true);
            field.set(obj, value);
            return field.get(obj);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Object call(Object obj, String className, String methodName, Class<?>[] parameterTypes, Object... paramters) {
        Object o = null;
        try {
            Class<?> cl = Class.forName(className);
            Method method = cl.getDeclaredMethod(methodName, parameterTypes);
            method.setAccessible(true);
            o = method.invoke(obj, paramters);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return o;
    }

    public static Object call(Object obj, Class cl, String methodName, Class<?>[] parameterTypes, Object... paramters) {
        Object o = null;
        try {
            Method method = cl.getDeclaredMethod(methodName, parameterTypes);
            method.setAccessible(true);
            o = method.invoke(obj, paramters);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return o;
    }

    public static Object get(Object obj, String className, String fieldName) {
        try {
            Class<?> cl = Class.forName(className);
            Field field = cl.getDeclaredField(fieldName);
            field.setAccessible(true);
            Object o = field.get(obj);
            return o;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}
